package popcorn.ticket.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import popcorn.ticket.entity.MovieHallType;
import popcorn.ticket.serviceimpl.MovieHallTypeServiceImpl;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

/**
 * 影厅控制器
 * 
 * @author lenovo
 *
 */
@RestController
public class MovieHallTypeController {
	@Autowired
	MovieHallTypeServiceImpl mhsi;

	/**
	 * 分页+模糊查询
	 * 
	 * @param page
	 * @param limit
	 * @param keywords
	 * @return
	 */
	@GetMapping(value = "selectMovieHallType", produces = "application/json;charset=utf-8")
	public PageData<MovieHallType> selectMovieHall(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords) {
		return mhsi.selectMovieHallTypeByPage(page, limit, keywords);
	}

	/**
	 * 获得所有的影厅类型
	 * 
	 * @return
	 */
	@GetMapping(value = "getMovieHallTypes", produces = "application/json;charset=utf-8")
	public List<MovieHallType> getMovieHallTypes() {
		return mhsi.getMovieHallTypes();
	}

	/**
	 * 添加
	 * 
	 * @param mh
	 * @return
	 */
	@PostMapping(value = "addMovieHallType", produces = "application/json;charset=utf-8")
	public AjaxResponse addMovieHall(@RequestBody MovieHallType mh) {

		int n = mhsi.addMovieHallType(mh);
		int code = 1;
		String msg = "添加成功！";
		if (n == 0) {
			code = 0;
			msg = "添加失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 修改（布局有变化）
	 * 
	 * @param mh
	 * @return
	 */
	@GetMapping(value = "updMovieHallType", produces = "application/json;charset=utf-8")
	public AjaxResponse updMovieHall(int row, int column, String halltypename, int halltypeid, String array) {
		//将影厅布局变成字符串
		List<String> list = new ArrayList<String>();
		if (array.length() != 0) {
			String[] seatStr = array.split(",");
			for (int i = 1; i <= row; i++) {
				String str = "";
				for (int j = 1; j <= column; j++) {
					int row1 = 0;
					int column1 = 0;
					boolean flag = false;
					for (int j2 = 0; j2 < seatStr.length; j2++) {
						String[] seatStr1 = seatStr[j2].split("_");
						row1 = Integer.parseInt(seatStr1[0]);
						column1 = Integer.parseInt(seatStr1[1]);
						if (row1 == i && column1 == j) {
							flag = true;
						}
					}
					if (flag) {
						str += "_";
					} else {
						str += "c";
					}
				}
				list.add(str);
			}
		} else {
			list = null;
		}
		MovieHallType mht = new MovieHallType();
		mht.setHalltypeid(halltypeid);
		mht.setGalleryful(list.toString());
		mht.setHalltypename(halltypename);
		int n = mhsi.updMovieHallType(mht);
		int code = 1;
		String msg = "修改成功！";
		if (n == 0) {
			code = 0;
			msg = "修改失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 修改（布局没有变化）
	 * 
	 * @param mh
	 * @return
	 */
	@GetMapping(value = "updMovieHallType1", produces = "application/json;charset=utf-8")
	public AjaxResponse updMovieHall1(String halltypename, int halltypeid) {
		MovieHallType mht = new MovieHallType();
		mht.setHalltypeid(halltypeid);
		mht.setHalltypename(halltypename);
		int n = mhsi.updMovieHallType(mht);
		int code = 1;
		String msg = "修改成功！";
		if (n == 0) {
			code = 0;
			msg = "修改失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 删除
	 * 
	 * @param mh
	 * @return
	 */
	@GetMapping(value = "delMovieHallType/{halltypeid}", produces = "application/json;charset=utf-8")
	public AjaxResponse delMovieHall(@PathVariable("halltypeid") int halltypeid) {

		int n = mhsi.delMovieHallType(halltypeid);
		int code = 1;
		String msg = "删除成功！";
		if (n == 0) {
			code = 0;
			msg = "删除失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@PostMapping(value = "delAllMovieHallType", produces = "application/json;charset=utf-8")
	public AjaxResponse delBlog(String array) {
		int n = mhsi.delAllMovieHallType(array);
		String msg = "成功删除了" + n + "条记录！";
		int code = 1;
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}

	/**
	 * 动态生成初始状态map
	 * 
	 * @param row
	 * @param column
	 * @return
	 */
	@GetMapping(value = "seat11", produces = "application/json;charset=utf-8")
	public List<String> getSeat1(int row, int column) {
		List<String> list = new ArrayList<String>();
		String str = "";
		for (int j = 0; j < column; j++) {
			str += "c";
		}
		for (int i = 0; i < row; i++) {
			list.add(str);
		}
		return list;
	}

	/**
	 * 动态生成map,存入数据库
	 * 
	 * @param row
	 * @param column
	 * @return
	 */
	@GetMapping(value = "seat22", produces = "application/json;charset=utf-8")
	public AjaxResponse getSeat2(String halltypename, int row, int column, String array) {

		String[] seatStr = array.split(",");
		List<String> list = new ArrayList<String>();
		for (int i = 1; i <= row; i++) {
			String str = "";
			for (int j = 1; j <= column; j++) {
				int row1 = 0;
				int column1 = 0;
				boolean flag = false;
				for (int j2 = 0; j2 < seatStr.length; j2++) {
					String[] seatStr1 = seatStr[j2].split("_");
					row1 = Integer.parseInt(seatStr1[0]);
					column1 = Integer.parseInt(seatStr1[1]);
					if (row1 == i && column1 == j) {
						flag = true;
					}
				}
				if (flag) {
					str += "_";
				} else {
					str += "c";
				}
			}
			list.add(str);
		}
		MovieHallType mht = new MovieHallType();
		mht.setGalleryful(list.toString());
		mht.setHalltypename(halltypename);
		int n = mhsi.addMovieHallType(mht);
		int code = 1;
		String msg = "添加成功！";
		if (n == 0) {
			code = 0;
			msg = "添加失败！";
		}
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
}
